package com.dhj.yygh.mongo_operation;

import com.dhj.yygh.model.hosp.Schedule;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ScheduleTemplate {
    @Autowired
    MongoTemplate template;

    /**
     * 根据医院编号和排班编号查询排班信息
     *
     * @param schedule 排班对象
     * @return 返回排班信息
     */
    public Schedule getScheduleByHoscodeAndHosScheduleId(Schedule schedule) {

        Query query = new Query(Criteria.where("hoscode").is(schedule.getHoscode()).
                and("hosScheduleId").is(schedule.getHosScheduleId()));
        return template.findOne(query, Schedule.class);
    }


    /**
     * 根据医院代码和排班代码删除排班信息
     *
     * @param hoscode     医院代码
     * @param hosShcedule 排班代码
     * @return 返回删除记录数
     */
    public Long removeScheduleByHoscodeAndHsoSchdule(String hoscode, String hosShcedule) {
        Query query = new Query(Criteria.where("hoscode").is(hoscode).and("hosShcedule").is(hosShcedule));

        Schedule schedule = template.findOne(query, Schedule.class);

        if (schedule != null) {
            String id = schedule.getId();

            // 按照 id 删除
            Query query1 = new Query(Criteria.where("_id").is(id));

            DeleteResult remove = template.remove(query1, Schedule.class);

            return remove.getDeletedCount();
        }
        return 0L;
    }

    public List<Schedule> getScheduleByHoscodeAndDepcode(String hoscode, String depcode) {
        Query query = new Query(Criteria.where("hoscode").is(hoscode).and("depcode").is(depcode));

        return template.find(query, Schedule.class);
    }
}
